package android.Graphics;

import javax.microedition.khronos.opengles.GL10;
import java.nio.FloatBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: akuryou
 * Date: 4/26/11
 * Time: 4:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class InitDiamond
{
    private float[] diamond;
    private int x;
    private int y;
    private int cellWidth;
    private int cellHeight;
    private int lineWidth;

    private FloatBuffer vertexBuffer;

    public InitDiamond( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        this.x = x;
        this.y = y;
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.lineWidth = lineWidth;
        initDiamond();
    }

    public void initDiamond()
    {
        diamond = diamondArray( x, y, cellWidth, cellHeight, lineWidth );
        vertexBuffer = FloatBuffer.allocate( diamond.length );
        vertexBuffer = FloatBuffer.wrap( diamond );
        vertexBuffer.put( diamond );
        vertexBuffer.position( 0 );
    }

    public float[] diamondArray( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        diamond = new float[8];

        //Top
        diamond[0] = x * cellWidth + cellWidth/2;
        diamond[1] = (y+1) * cellHeight - 3*lineWidth;
        //Left
        diamond[2] = x * cellWidth + 3*lineWidth;
        diamond[3] = y * cellHeight + cellHeight/2;
        //Bottom
        diamond[4] = diamond[0];
        diamond[5] = y * cellHeight + 3*lineWidth;
        //Right
        diamond[6] = (x+1) * cellWidth - 3*lineWidth;
        diamond[7] = diamond[3];

        return diamond;
    }

    public void drawDiamond( GL10 gl )
    {
        gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertexBuffer );
        gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
        gl.glDrawArrays( GL10.GL_LINE_LOOP, 0, diamond.length/2 );
    }
}
